﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    public class LoadControlMap : EntityTypeConfiguration<LoadControl> {
        public LoadControlMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.eod_load_controls");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(a => a.LoadControlId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            // IX_EOD_LCL_CD
            Property(a => a.ExchangeId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_EOD_LCL_CD", 1) { IsUnique = true }));
            Property(a => a.LoadTypeId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_EOD_LCL_CD", 2) { IsUnique = true }));

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // n..1 Exchange
            HasRequired<Exchange>(a => a.Exchange)
                .WithMany(b => b.LoadControls)
                .HasForeignKey(b => b.ExchangeId)
                .WillCascadeOnDelete(false);

            // n..1 Frequency
            HasRequired<Frequency>(a => a.Frequency)
                .WithMany()
                .HasForeignKey(a => a.FrequencyId)
                .WillCascadeOnDelete(false);

            // n..1 LoadType
            HasRequired<LoadType>(a => a.LoadType)
                .WithMany()
                .HasForeignKey(a => a.LoadTypeId)
                .WillCascadeOnDelete(false);

            // n..1 Load
            HasMany<Load>(a => a.Loads)
                .WithRequired(a => a.LoadControl)
                .HasForeignKey(a => a.LoadControlId);

            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(a => a.LoadControlId)
                    .IsRequired()
                    .HasColumnName("lcl_id");

            Property(a => a.ExchangeId)
                .IsRequired()
                .HasColumnName("lcl_exc_id");

            Property(a => a.LoadTypeId)
                .IsRequired()
                .HasColumnName("lcl_cat_load_type_id");

            Property(a => a.FrequencyId)
                .IsRequired()
                .HasColumnName("lcl_cat_frequncy_id");

            Property(a => a.TimeOffset)
                .IsRequired()
                .HasColumnName("lcl_load_offset");
        }
    }
}
